// page/detail/pages/distributor/distributor.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 5,
    start_date: "",
    end_date: '',
    listData: [],
  },
  search() {
    const that = this;
    if (!that.data.start_date) {
      wx.showToast({
        title: '请选择开始时间',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!that.data.end_date) {
      wx.showToast({
        title: '请选择结束时间',
        icon: 'none',
        duration: 1000
      })
      return
    }
    that.getData()
  },
  bindStartDate(e) {
    const that = this;
    that.setData({
      start_date: e.detail.value
    })
  },
  bindEndDate(e) {
    const that = this;
    that.setData({
      end_date: e.detail.value
    })
  },
  getData() {
    const that = this;
    const token = app.globalData.token;
    let json = {
      page: that.data.page,
      limit: that.data.pageSize,
      start_date: that.data.start_date,
      end_date: that.data.end_date,
      token: token,
    }
    let data = app.utils.getSignData(json); // 添加签名
    wx.showLoading({
      title: '加载中...',
    })
    app.utils.post(app.api.reqUrls.getSubordinateOrder, data).then((res) => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.status == 1) {
        if (res.result.list.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          that.setData({
            isWithout: true,
            listData: res.result.list
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
      start_date: '',
      end_date: '',
    })
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    const token = app.globalData.token;
    let json = {
      page: that.data.page + 1,
      limit: that.data.pageSize,
      start_date: that.data.start_date,
      end_date: that.data.end_date,
      token: token,
    }
    let data = app.utils.getSignData(json); // 添加签名
    wx.showLoading({
      title: '加载中...',
    })
    app.utils.post(app.api.reqUrls.getSubordinateOrder, data).then((res) => {
      wx.hideLoading();

      if (res.status == 1) {
        if (res.result.list.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          that.setData({
            listData: [...that.data.listData, ...res.result.list],
            page: that.data.page + 1
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})